package com.joinpay.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.joinpay.model.Group;
import com.joinpay.service.GroupService;

@Controller
public class GroupController {

	@Resource
	private GroupService groupService;

	/**
	 * 首次登陆，获取汇聚支付
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "getRootgGroup", method = RequestMethod.POST)
	@ResponseBody
	public String getRootgGroup(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		Map<String, Object> mapEntity = new HashMap<String, Object>();
		try {
			mapEntity.put("uID", "rootbranch");
			List<Group> groupList = groupService.getGroupList(mapEntity);
			String username = (String) request.getSession().getAttribute("username");
			mapSend.put("groupList", groupList);
			mapSend.put("username", username);
			mapSend.put("flag", "success");
		} catch (Exception e) {
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}

	/**
	 * 添加组别
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "addGroup", method = RequestMethod.POST)
	@ResponseBody
	public String addGroup(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		try {
			String username = (String) request.getSession().getAttribute("username");
			int i = groupService.addGroup(username,request.getParameter("uID"), request.getParameter("groupName"));
			mapSend.put("flag", "success");
			if (i == 0) {
				mapSend.put("flag", "fail");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}

	/**
	 * 更新组别信息
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "updateGroup", method = RequestMethod.POST)
	@ResponseBody
	public String updateGroup(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		try {
			String groupName = (String) request.getSession().getAttribute("username");
			int i = groupService.updateGroup(groupName,request.getParameter("id"), request.getParameter("groupName"));
			mapSend.put("flag", "success");
			if (i == 0) {
				mapSend.put("flag", "fail");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}

	/**
	 * 获取子组别信息
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "getChildGroup", method = RequestMethod.POST)
	@ResponseBody
	public String getChildGroup(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		Map<String, Object> mapEntity = new HashMap<String, Object>();
		try {
			mapEntity.put("uID", request.getParameter("id"));
			List<Group> groupList = groupService.getGroupList(mapEntity);
			if (groupList.size() == 0) {
				mapSend.put("flag", "fail");
			} else {
				mapSend.put("flag", "success");
				mapSend.put("groupList", groupList);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}

	/**
	 * 删除组别信息
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "deleteGroup", method = RequestMethod.POST)
	@ResponseBody
	public String deleteGroup(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		try {
			int i = groupService.deleteGroup(request.getParameter("id"));
			if (i == 0) {
				mapSend.put("flag", "fail");
			} else {
				mapSend.put("flag", "success");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}

	/**
	 * 获取完整的组别名
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "getCompleteGroup", method = RequestMethod.POST)
	@ResponseBody
	public String getCompeteGroup(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		try {
			String groupName = groupService.getCompleteGroup(request.getParameter("id"));
			mapSend.put("groupName", groupName);
			mapSend.put("flag", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}
}
